
#include <unistd.h>
#include <errno.h>

#include "MobsBuf.h"

int main(int argc,char *argv[])
{
    int ret = 0,roid;
    uintptr_t ruid;

    mobs_unlink("/tmp/buf02.mobs");

    char user_name[32];
    snprintf(user_name,sizeof(user_name),"%s",argv[0]);
    if(argc >= 2)
    {
	snprintf(user_name,sizeof(user_name),"cons_%s",argv[1]);
    }
    ruid = mobs_open(user_name,"/tmp/buf02.mobs",
	    MOBS_FLAG_CREATE|MOBS_FLAG_RDONLY|MOBS_FLAG_SHARED,1024*1024,1024,1024);
    printf("open mobs ruid = 0x%08lx\n",ruid);

    if(ruid == -1) exit(EXIT_FAILURE);

    char data[1024]="hello mobs!";
    while(1)
    { //test read
	ret = mobs_start(ruid,0);
	printf("~~~read start ret = %d\n",ret);
	if(ret == MOBS_ERR_EMPTY)
	{
	    usleep(500*1000);
	    continue;
	}
	else if(ret < 0) exit(EXIT_FAILURE);

	roid = ret;
	memset(data,0,sizeof(data));
	ret = mobs_read(ruid,roid,data,sizeof(data));
	printf("###read data:%s ret = %d\n",data,ret);
	if(ret == MOBS_ERR_INTERRUPTED)
	{// next
	}
	else if(ret < 0)
	{
	    exit(EXIT_FAILURE);
	}
	mobs_finish(ruid,roid);
    }

    mobs_close(ruid);
    printf("close mobs ruid = 0x%08lx\n",ruid);

    mobs_unlink("/tmp/buf02.mobs");
    return 0;
}

